package com.example.xjtuse_pc.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.xjtuse_pc.services.AddressService;
import com.example.xjtuse_pc.services.BlackNumberSmsService;
import com.example.xjtuse_pc.services.GPSService;
import com.example.xjtuse_pc.services.WatchDogService;
import com.example.xjtuse_pc.ui.SettingClickView;
import com.example.xjtuse_pc.ui.SettingItemView;
import com.example.xjtuse_pc.utils.SeviceUtils;

public class SettingActivity extends Activity {
    private SettingItemView siv_update, siv_show_address, siv_black_number,siv_app_lock;
    private SharedPreferences sp;
    private Intent showAddress,callSmsSafeIntent,WatchDog;
    private SettingClickView scv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        siv_update = (SettingItemView) findViewById(R.id.siv_update);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        boolean update = sp.getBoolean("update", false);
        if (update) {
            siv_update.setChecked(true);

        } else {
            siv_update.setChecked(false);

        }
        siv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor ed = sp.edit();
                if (siv_update.isChecked()) {
                    siv_update.setChecked(false);
                    ed.putBoolean("update", false);
                } else {
                    siv_update.setChecked(true);
                    ed.putBoolean("update", true);
                }
                ed.commit();
            }
        });

        //设置黑名单拦截
        callSmsSafeIntent = new Intent(this, BlackNumberSmsService.class);
        siv_black_number= (SettingItemView) findViewById(R.id.siv_black_number);
        Boolean blackresult = SeviceUtils.isServiceRunning(SettingActivity.this, "com.example.xjtuse_pc.services.BlackNumberSmsService");
        if (blackresult) {
            //服务在运行
            siv_black_number.setChecked(true);
        } else {
            siv_black_number.setChecked(false);
        }
        siv_black_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siv_black_number.isChecked()) {
                    //变为非选中
                    siv_black_number.setChecked(false);
                    stopService(callSmsSafeIntent);

                } else {
                    //变为选中
                    siv_black_number.setChecked(true);
                    startService(callSmsSafeIntent);
                }
            }
        });

        //设置号码归属地显示
        showAddress = new Intent(SettingActivity.this, AddressService.class);
        siv_show_address = (SettingItemView) findViewById(R.id.siv_show_address);
        Boolean result = SeviceUtils.isServiceRunning(SettingActivity.this, "com.example.xjtuse_pc.services.AddressService");
        if (result) {
            //服务在运行
            siv_show_address.setChecked(true);
        } else {
            siv_show_address.setChecked(false);
        }
        siv_show_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siv_show_address.isChecked()) {
                    //变为非选中
                    siv_show_address.setChecked(false);
                    stopService(showAddress);

                } else {
                    //变为选中
                    siv_show_address.setChecked(true);
                    startService(showAddress);
                }
            }
        });



        //设置程序锁
        WatchDog = new Intent(SettingActivity.this, WatchDogService.class);
        siv_app_lock = (SettingItemView) findViewById(R.id.siv_app_lock);
        Boolean result1 = SeviceUtils.isServiceRunning(SettingActivity.this, "com.example.xjtuse_pc.services.WatchDogService");
        if (result1) {
            //服务在运行
            siv_app_lock.setChecked(true);
        } else {
            siv_app_lock.setChecked(false);
        }
        siv_app_lock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siv_app_lock.isChecked()) {
                    //变为非选中
                    siv_app_lock.setChecked(false);
                    stopService(WatchDog);

                } else {
                    //变为选中
                    siv_app_lock.setChecked(true);
                    startService(WatchDog);
                }
            }
        });






        //设置号码归属地背景
        scv = (SettingClickView) findViewById(R.id.scv_changback);
        scv.setTitle("归属地提示框风格");
        final String[] items = {"半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿"};
        int which = sp.getInt("which", 0);
        scv.setDesc(items[which]);
        scv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int w = sp.getInt("which", 0);
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("归属地提示框风格");
                builder.setSingleChoiceItems(items, w, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor ed = sp.edit();
                        ed.putInt("which", which);
                        ed.commit();
                        scv.setDesc(items[which]);
                        //取消对话框
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("取消选择", null);
                builder.show();

            }
        });
    }


}
